'use strict';//首页轮播图
const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
class first_rotation_chartController extends Controller {
    async index() {
        const { ctx, app } = this;
        const result = await app.mysql.query(`select * from first_rotation_chart order by sort `)
        ctx.body = {
            code: 0,
            msg: "请求成功",
            data: result
        };
    }
    async create() {
        const { ctx, app } = this;
        const { img, sort } = ctx.request.body;
        const count = await app.mysql.query(`select count(*) from  first_rotation_chart`);
        let nummax = 0;
        if (count[0].count !== 0) {
            const show = await app.mysql.query(`select sort from  first_rotation_chart`);
            if (sort) {
                const result = show.find(item => item.sort == parseInt(sort));
                if (result) {
                    for (const item of show) {
                        if (item.sort >= sort) {
                            const num = item.sort + 1;
                            const row = {
                                sort: num
                            }
                            const options = {
                                where: {
                                    sort: item.sort
                                }
                            }
                            const data = await app.mysql.update('first_rotation_chart', row, options);
                        }
                    }
                    nummax = sort;
                } else {
                    nummax = sort;
                }
            } else {
                show.find(function (item) {
                    if (item.sort > nummax) {
                        nummax = item.sort;
                    }
                });
                nummax += 1;
            }
        } else {
            nummax = 1;
        }
        if (img) {
            const data = await app.mysql.insert('first_rotation_chart', { img, sort: nummax });
            ctx.body = {
                code: 0,
                msg: "插入成功"
            }
        } else {
            ctx.body = {
                code: 406,
                msg: "插入失败"
            }
        }

    }


    async update() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const { img, sort } = ctx.request.body;
        const idd = await app.mysql.query(`select id from first_rotation_chart`)
        const result = idd.find(item => item.id === +id)
        if (result) {
            let row1;
            if (sort) {
                const show = await app.mysql.query(`select id from first_rotation_chart where sort=${sort}`);
                if (show.length !== 0) {
                    const show1 = await app.mysql.query(`select sort from first_rotation_chart where id=${id}`)
                    const row = {
                        sort: show1[0].sort
                    };
                    const options = {
                        where: {
                            id: show[0].id
                        }
                    };
                    const show2 = await app.mysql.update(`first_rotation_chart`, row, options);
                }
                row1 = {
                    img,
                    sort
                };
            } else {
                row1 = {
                    img, _describe
                }
            }
            const options1 = {
                where: {
                    id: +id
                }
            };
            const data = await app.mysql.update(`first_rotation_chart`, row1, options1)
            const path = await app.mysql.query(`select 	img from first_rotation_chart where id=${id}`);
            if (path[0].img!==img) {
                fs.unlink(`app/${path[0].img}`, (err) => {
                    if (err) throw err;
                });
            }
            ctx.body = {
                code: 0,
                msg: "修改成功"
            }
        } else {
            ctx.body = {
                code: 4007,
                msg: "修改失败"
            }
        }

    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`first_rotation_chart`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = first_rotation_chartController;

